Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Egress Traffic Tracking + Stripe Billing Meters #430

Merged
merged 15 commits into from
Oct 30, 2024

Conversation

fforbeck
Copy link
Member

@fforbeck fforbeck commented Oct 17, 2024

Egress Traffic Tracking and Stripe Billing Meters API Integration

This PR introduces an asynchronous solution for tracking egress traffic in the Freeway Gateway and reporting events to Stripe’s Billing Meters API.

RFC Reference

Storacha Network is implementing a scalable, automated mechanism for tracking egress traffic and updating Stripe’s API with relevant data to ensure accurate customer billing. This RFC outlines the proposed approaches and their trade-offs.

Implementation

The selected approach for this implementation is Alternative 3:

Flow

  1. To avoid blocking, the Freeway Gateway asynchronously invokes the usage/record capability using ctx.waitUntil.
  2. A new handler added to w3infra/upload-api/stores/usage.js receives the events from the Kinesis stream and places the egress data into an Egress Traffic SQS queue.
  3. A new Lambda function consumes the events from the SQS queue and publishes the egress data to the Stripe Billing Meters API.
graph TD
    CF[Freeway Gateway] --> w3infra[w3infra/upload-api]
    w3infra --> Kinesis[Kinesis Stream]
    Kinesis --> UsageRecord[Usage Record Handler]
    UsageRecord --> SQS[Egress Traffic Queue]
    SQS --> Lambda[Lambda Function]
    Lambda --> DynamoDB[Egress Traffic DynamoDB Table]
    Lambda --> Stripe[Stripe Billing Meters API]
    UsageRecord --> Logs[S3 Bucket /Receipts]
Loading

Key Advantages

  • Asynchronous execution: ctx.waitUntil ensures the Freeway Worker won’t block, allowing seamless egress traffic handling.
  • Decoupled architecture: By using SQS and Lambda, the architecture is decoupled and includes a buffer before publishing events to Stripe.
  • Signed receipts: Each egress event generates a signed receipt, providing auditability and traceability. The receipts are stored in the general bucket in S3 where we already store other receipts.

Summary of Changes

  • Introduces the usage/record capability handler in w3infra/upload-api/stores/usage.js to handle egress data from the Kinesis stream and place them into the SQS queue.
  • Implements an event-driven architecture using SQS and Lambda to process egress events asynchronously and publish to Stripe.
  • An integration test that publishes fake egress traffic events on Stripe’s Test API.

Next Steps

  • Deployment (Staging)
  • Validate the solution’s performance and scalability.
  • Monitor for potential rate limit issues when publishing to Stripe.

@fforbeck fforbeck mentioned this pull request Oct 17, 2024
7 tasks
@fforbeck fforbeck force-pushed the feat/egress-tracking-3 branch from afffed6 to 5f86e7d Compare October 17, 2024 13:41
@fforbeck fforbeck force-pushed the feat/egress-tracking-3 branch from 2194d13 to 8ed550e Compare October 17, 2024 19:37
@fforbeck fforbeck marked this pull request as ready for review October 18, 2024 18:31
@fforbeck fforbeck changed the title feat: egress traffic tracker feat: Egress Traffic Tracking + Stripe Billing Meters Oct 18, 2024
@fforbeck fforbeck force-pushed the feat/egress-tracking-3 branch from c389ce8 to f692f12 Compare October 21, 2024 21:45
@fforbeck fforbeck force-pushed the feat/egress-tracking-3 branch 2 times, most recently from 77cd722 to b8275dd Compare October 21, 2024 21:50
@fforbeck fforbeck force-pushed the feat/egress-tracking-3 branch from b8275dd to c5223d8 Compare October 21, 2024 22:02
travis
travis previously requested changes Oct 22, 2024
Copy link
Member

@travis travis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few changes needed and some TODOs that need to be addressed but generally great stuff! excited to use the new billing meters!

billing/data/egress.js Show resolved Hide resolved
billing/functions/egress-traffic-handler.js Outdated Show resolved Hide resolved
billing/functions/egress-traffic-handler.js Outdated Show resolved Hide resolved
billing/functions/egress-traffic-handler.js Outdated Show resolved Hide resolved
billing/functions/egress-traffic-handler.js Outdated Show resolved Hide resolved
billing/functions/egress-traffic-handler.js Outdated Show resolved Hide resolved
stacks/billing-stack.js Outdated Show resolved Hide resolved
upload-api/stores/usage.js Show resolved Hide resolved
Copy link

seed-deploy bot commented Oct 25, 2024

View stack outputs

Copy link
Member

@hannahhoward hannahhoward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM with one big comment:

I don't think we will have the customer DID when we call usage/record in freeway. @Peeja can you confirm that I'm right on that? Or maybe @travis has more context.

I'm sorry I didn't catch this earlier. You should be able to look it up via the subscription for the space.

@fforbeck
Copy link
Member Author

fforbeck commented Oct 28, 2024

Generally LGTM with one big comment:

I don't think we will have the customer DID when we call usage/record in freeway. @Peeja can you confirm that I'm right on that? Or maybe @travis has more context.

I'm sorry I didn't catch this earlier. You should be able to look it up via the subscription for the space.

Thanks for the review. No problem. That's right @hannahhoward , we won't have the Customer DID in the Freeway project, but we will have the Space DID, and this is what we will pass in the invocation. The w3up project was updated to derive the Customer DID from the Space+Provider: https://github.com/storacha/w3up/blob/main/packages/upload-api/src/usage/record.js#L18, and then it pushes the event to the queue with the Customer DID expected in the w3infra.

@hannahhoward
Copy link
Member

Perfect block removed

@fforbeck fforbeck dismissed travis’s stale review October 28, 2024 17:03

Most of the suggested changes were implemented.

@fforbeck fforbeck merged commit 85cf83a into main Oct 30, 2024
3 checks passed
@fforbeck fforbeck deleted the feat/egress-tracking-3 branch October 30, 2024 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants